//
//  ShareObj.m
//  HDShangHaiPlanningMuseum
//
//  Created by liuyi on 2017/6/15.
//  Copyright © 2017年 牛的mini. All rights reserved.
//

#import "ShareObj.h"
#import <UShareUI/UShareUI.h>

@interface ShareObj ()<UMSocialShareMenuViewDelegate>

@end

@implementation ShareObj

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupShareView];
    }
    return self;
}

- (void)setupShareView {
    
    //设置用户自定义的平台
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),
                                               @(UMSocialPlatformType_WechatTimeLine),
                                               @(UMSocialPlatformType_QQ),
                                               @(UMSocialPlatformType_Qzone),
                                               ]];
    //设置分享面板的显示和隐藏的代理回调
    [UMSocialUIManager setShareMenuViewDelegate:self];
}

#pragma mark - UMSocialShareMenuViewDelegate
- (void)UMSocialShareMenuViewDidAppear
{
    //NSLog(@"UMSocialShareMenuViewDidAppear");
}
- (void)UMSocialShareMenuViewDidDisappear
{
    //NSLog(@"UMSocialShareMenuViewDidDisappear");
}

//不需要改变父窗口则不需要重写此协议
- (UIView*)UMSocialParentView:(UIView*)defaultSuperView
{
    UIWindow *win = kKeyWindow;
    UIView *view = [[UIView alloc] initWithFrame:win.bounds];
    view.backgroundColor = [UIColor blueColor];
    [win addSubview:view];
    
    return view;
}


#pragma mark --- show ---
//样式一：页面底部菜单-1
- (void)showBottomNormalView
{
    declare = [HDDeclare sharedDeclare];
    //加入copy的操作
    //@see http://dev.umeng.com/social/ios/进阶文档#6
    
    //自定义图片和标题
    //微信
//    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_WechatSession
//                                     withPlatformIcon:[UIImage imageNamed:@"umsocial_wechat"]
//                                     withPlatformName:declare.weixin];
//    //朋友圈
//    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_WechatTimeLine
//                                     withPlatformIcon:[UIImage imageNamed:@"umsocial_wechat_timeline"]
//                                     withPlatformName:declare.wxTimeline];
//    //QQ
//    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_QQ
//                                     withPlatformIcon:[UIImage imageNamed:@"umsocial_qq"]
//                                     withPlatformName:declare.qq];
//    //QQ空间
//    [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_Qzone
//                                     withPlatformIcon:[UIImage imageNamed:@"umsocial_qzone"]
//                                     withPlatformName:declare.qqzone];
    //自定义显示样式
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;
    //自定义标题显示
//  [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.shareTitleViewTitleString = declare.sharePlatform;
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlText = @"取消";

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
            //在回调里面获得点击的
            if (platformType == UMSocialPlatformType_UserDefine_Begin+2) {
                NSLog(@"点击演示添加Icon后该做的操作");
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"添加自定义icon"
                                                                    message:@"具体操作方法请参考UShareUI内接口文档"
                                                                   delegate:nil
                                                          cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                          otherButtonTitles:nil];
                    [alert show];
                });
            }
            else{
                [self runShareWithType:platformType];
            }
        }];
}

- (void)showBottomCircleView {
        [UMSocialUIManager removeAllCustomPlatformWithoutFilted];
        [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_WechatSession
                                         withPlatformIcon:[UIImage imageNamed:@"umsocial_wechat"]
                                         withPlatformName:@""];
        //朋友圈
        [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_WechatTimeLine
                                         withPlatformIcon:[UIImage imageNamed:@"umsocial_wechat_timeline"]
                                         withPlatformName:@""];
        //QQ
        [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_QQ
                                         withPlatformIcon:[UIImage imageNamed:@"umsocial_qq"]
                                         withPlatformName:@""];
        //QQ空间
        [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_Qzone
                                         withPlatformIcon:[UIImage imageNamed:@"umsocial_qzone"]
                                         withPlatformName:@"" ];
        [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;
    [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerBackgroundColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewBackgroundColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewBackgroundColor = [UIColor whiteColor];

        [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_IconAndBGRadius;
        
        [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                [self runShareWithType:platformType];
            }];
}
         
- (void)showMiddleNormalView {
    [UMSocialUIManager removeAllCustomPlatformWithoutFilted];
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Middle;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_None;

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                    [self runShareWithType:platformType];
                }];
}

- (void)showMiddleCircleView {
    [UMSocialUIManager removeAllCustomPlatformWithoutFilted];
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Middle;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_IconAndBGRadius;
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
                        [self runShareWithType:platformType];
    }];
    
}
- (void)shareWithType:(NSInteger)type {
    if (type == 1) {
        [self runShareWithType:UMSocialPlatformType_QQ];
    }
    else if (type == 2) {
        [self runShareWithType:UMSocialPlatformType_WechatTimeLine];
    }
    else if (type == 3) {
        [self runShareWithType:UMSocialPlatformType_Sina];
    }
}

                 
- (void)runShareWithType:(UMSocialPlatformType)type {
    
    //创建分享消息对象
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //创建网页内容对象
    UIImage *img = _shareImg;
    if (!img) {
        img = [UIImage imageNamed:@"shareImg"];
    }
    UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:_title descr:_web_Desc thumImage:img];
    //设置网页地址
    shareObject.webpageUrl = _webLink;
    
    //分享消息对象设置分享内容对象
    messageObject.shareObject = shareObject;    //设置文本
//    messageObject.text = @"分享结果消息";
    //调用分享接口
    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:declare.visibleVC completion:^(id data, NSError *error) {
        NSString *msg;
            if (error)
            {
                LOG(@"************Share fail with error %@*********",error);
            }else
            {
                if ([data isKindOfClass:[UMSocialShareResponse class]])
                {
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    LOG(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    LOG(@"response originalResponse data is %@",resp.originalResponse);
                }else{
                    LOG(@"response data is %@",data);
                }
            }
            [self alertWithError:error withType:type];
        [kNotificationCenter postNotificationName:kClose_ShareView_Noti object:nil];
        }];
}
     
- (void)alertWithError:(NSError *)error withType:(UMSocialPlatformType)type
{
    NSString *result = nil;
    if (!error) {
        result = [HDDeclare UIString:@"share_succeed"];
        [LCCoolHUD showSuccess:result zoom:NO shadow:NO];
        return;
    }else{
        if (error.code == 2000) {
            result = @"未知错误";
        }else if (error.code == 2001)
        {
            result = @"未安装软件或版本不支持";
        }else if (error.code == 2002)
        {
            result = @"授权失败";
        }else if (error.code == 2003)
        {
            result = @"分享失败";
        }else if (error.code == 2005)
        {
            result = @"分享内容为空";
        }else if (error.code == 2008)
        {
            if (type == UMSocialPlatformType_WechatSession || type == UMSocialPlatformType_WechatTimeLine) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E5%BE%AE%E4%BF%A1/id414478124?mt=8"]];
                
            } else if(type == UMSocialPlatformType_Sina) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E5%BE%AE%E5%8D%9A/id350962117?mt=8"]];
            }
            else if(type == UMSocialPlatformType_QQ) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/qq/id444934666?mt=8"]];
            }
            
            return;
        }else if (error.code == 2009)
        {
            result = @"分享取消";
        }else if (error.code == 2010)
        {
            result = @"网络错误";
        }else {
            result = [NSString stringWithFormat:@"%@",@"分享失败"];
        }
    }
    
    [LCCoolHUD showFailure:result zoom:NO shadow:NO];
}

- (void)alertWithError:(NSError *)error {
    declare = [HDDeclare sharedDeclare];
        NSString *result = nil;
        if (!error) {
            result = [HDDeclare UIString:@"share_success"];
            [HDDeclare showSuccessTip:result];
        }else
        {
//            NSMutableString *str = [NSMutableString string];
//            if (error.userInfo) {
//                for (NSString *key in error.userInfo) {
//                    [str appendFormat:@"%@ = %@\n", key, error.userInfo[key]];
//                }
//            }
//            if (error) {
//                result = [NSString stringWithFormat:@"Share fail with error code: %d\n%@",(int)error.code, str];
//            }
//            else{
//            }
            if (error.code == 2000) {
                result = [HDDeclare UIString:@"share_fail"];
            }else if (error.code == 2001)
            {
                result = [HDDeclare UIString:@"share_error"];
            }else if (error.code == 2002)
            {
                result = [HDDeclare UIString:@"share_fail"];
            }else if (error.code == 2003)
            {
                result = [HDDeclare UIString:@"share_fail"];
            }else if (error.code == 2005)
            {
                result = [HDDeclare UIString:@"share_fail"];
            }else if (error.code == 2008)
            {
                result = [HDDeclare UIString:@"share_error"];
            }else if (error.code == 2009)
            {
                result = [HDDeclare UIString:@"share_cancle"];
            }else if (error.code == 2010)
            {
                result = [HDDeclare UIString:@"share_fail"];
            }else {
                result = [HDDeclare UIString:@"share_fail"];
            }
            [HDDeclare showErrorTip:@"分享失败"];
        }
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
//                                                        message:result
//                                                       delegate:nil
//                                              cancelButtonTitle:@"确定"
//                                              otherButtonTitles:nil];
//        [alert show];
}
     
/*
 UMSocialPlatformErrorType_Unknow            = 2000,            // 未知错误
 UMSocialPlatformErrorType_NotSupport        = 2001,            // 不支持（url scheme 没配置，或者没有配置-ObjC， 或则SDK版本不支持或则客户端版本不支持）
 UMSocialPlatformErrorType_AuthorizeFailed   = 2002,            // 授权失败
 UMSocialPlatformErrorType_ShareFailed       = 2003,            // 分享失败
 UMSocialPlatformErrorType_RequestForUserProfileFailed = 2004,  // 请求用户信息失败
 UMSocialPlatformErrorType_ShareDataNil      = 2005,             // 分享内容为空
 UMSocialPlatformErrorType_ShareDataTypeIllegal = 2006,          // 分享内容不支持
 UMSocialPlatformErrorType_CheckUrlSchemaFail = 2007,            // schemaurl fail
 UMSocialPlatformErrorType_NotInstall        = 2008,             // 应用未安装
 UMSocialPlatformErrorType_Cancel            = 2009,             // 取消操作
 UMSocialPlatformErrorType_NotNetWork        = 2010,             // 网络异常
 UMSocialPlatformErrorType_SourceError       = 2011,             // 第三方错误
 
 UMSocialPlatformErrorType_ProtocolNotOverride = 2013,   // 对应的    UMSocialPlatformProvider的方法没有实现
 UMSocialPlatformErrorType_NotUsingHttps      = 2014,   // 没有用https的请求,@see UMSocialGlobal isUsingHttpsWhenShareContent
 
 
 */











@end














